package com.jackchen.service.impl;

import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.jackchen.common.transform.TransformException;
import com.jackchen.common.transform.Transformer;
import com.jackchen.dao.UserDAO;
import com.jackchen.po.User;
import com.jackchen.po.Usergroup;
import com.jackchen.service.UserService;
import com.jackchen.util.DateUtils;
import com.jackchen.vo.EditUserVO;
import com.jackchen.vo.UserManagementVO;

public class UserServiceImpl extends IBaseServiceImpl implements UserService {
	private static final Logger log = Logger.getLogger(UserServiceImpl.class);

	private UserDAO userDAO;
	
	public User queryUserByUserName(String userName,String password){
		User user = userDAO.findByUserNameAndPassword(userName,password);
		return user;
	}
	
	@Override
	public Map queryUserOnPage(String keyword, String regionGroupId,
			String userGroupId, String onlineState, int pageSize, int startRow) {
		Map userMap = userDAO.findUserOnPage(keyword, regionGroupId, userGroupId, onlineState, pageSize, startRow);
		List userList= (List<User>) userMap.get("querylist");
		List<UserManagementVO> dataList = null;
		try {
			dataList = Transformer.pojoToVos(userList, UserManagementVO.class);
		} catch (TransformException e) {
			// 数据转换失败
			log.error(e.getMessage());
			dataList = Collections.emptyList();
		}
		userMap.put("querylist", dataList);
		return userMap;
	}
	
	@Override
	public boolean verifyUsername(String username) {
		User user = userDAO.findByUserName(username);
		if (user == null) {
			return true;
		}
		return false;
	}
	
	@Override
	public boolean createNewUser(String username, String password,
			String usergroup, String realname, String phone, String regiongroupId,
			String email, int validDay, int languageFlag) {
		Usergroup userGroup = userDAO.findById(Usergroup.class, usergroup);
		Calendar curCal = Calendar.getInstance();
		
		User newUser = new User();
		newUser.setUsername(username);
		newUser.setPassword(password);
		newUser.setUsergroup(userGroup);
		newUser.setRealname(realname);
		newUser.setPhone(phone);
		newUser.setEmail(email);
		newUser.setCreatetime(curCal.getTime());//modify by daish
		newUser.setValidity(validDay);
		newUser.setLanguageflag(languageFlag); // 语言--0:英语; 1:汉语
		newUser.setOnline(false); // 是否在线 true：在线 false：不在线
		
		userDAO.save(newUser);
		return true;
	}
	
	@Override
	public Object[] deleteUsers(String[] userIds) {
		if (userIds == null || userIds.length == 0) {
			return null;
		}
		
		// 不能删除在线用户
		
		// 只能删除比当前用户权限低的用户
		
		// 删除用户后，3天内可以由管理员恢复删除账号
		
		int result = userDAO.deleteUsers(userIds);
		if (result == userIds.length) {
			return new Object[]{"success"};
		} else {
			return new Object[]{"db"};
		}
	}
	
	@Override
	public EditUserVO queryEditUser(String userId) {
		User user = userDAO.findById(User.class, userId);
		if (user == null) {
			return null;
		}
		try {
			return Transformer.pojoToVo(user, EditUserVO.class);
		} catch (TransformException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	
	@Override
	public boolean updateUser(String userId, String username, String usergroupId, 
			String realname, String phone, String regiongroupId, String email, 
			int validDay, int languageFlag) {
		User user = userDAO.findById(User.class, userId);
		Usergroup userGroup = userDAO.findById(Usergroup.class, usergroupId);
		if (userGroup == null || user == null) {
			return false;
		}
		
		user.setUsername(username);
		user.setUsergroup(userGroup);
		user.setRealname(realname);
		user.setPhone(phone);
		user.setEmail(email);
		user.setValidity(validDay);
		user.setLanguageflag(languageFlag); // 语言--0:英语; 1:汉语
		user.setOnline(false); // 是否在线 1：在线 0：不在线
		
		userDAO.update(user);
		return true;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

}
